package com.imay.live.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.imay.base.activity.BaseActivity;
import com.imay.live.helper.DialogFragmentHelper;
import com.imay.live.manager.FamilyAccountManager;
import com.imay.live.model.FamilyUserInfo;
import com.imay.live.model.PushConfigModel;
import com.imay.live.proto.Result;
import com.imay.live.proto.ResultNoData;
import com.imay.live.proto.helper.ProtoCallBack;
import com.imay.live.proto.helper.ProtoHttpFactory;
import com.imay.live.proto.helper.ProtoRequestHelper;
import com.imay.live.R;
import com.netease.nim.uikit.cache.TeamDataCache;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.RequestCallback;
import com.netease.nimlib.sdk.team.TeamService;
import com.netease.nimlib.sdk.team.model.Team;

import static com.imay.live.proto.ResultCode.CodeSuccess;

public class PushSettingActivity extends BaseActivity implements ProtoCallBack {


    private String loadPushConfigCommand;
    private String setPushConfigCommand;
    private CheckBox cb_live,cb_follow,cb_call_live,cb_family_message;
    private PushConfigModel pushConfigModel;

    public static void start(Context context) {
        Intent starter = new Intent(context, PushSettingActivity.class);
//        starter.putExtra();
        context.startActivity(starter);
    }
    @Override
    protected int getLayoutId() {
        return R.layout.activity_push_setting;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setTitle(getString(R.string.setting_notice));
        cb_live = $(R.id.cb_live);
        cb_follow = $(R.id.cb_follow);
        cb_call_live = $(R.id.cb_call_live);
        cb_family_message = $(R.id.cb_family_message);
        loadpushConfig();
        initFamilyMute();
        cb_live.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setPushConfig();

            }
        });
        cb_follow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setPushConfig();

            }
        });
        cb_call_live.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setPushConfig();

            }
        });


    }

    private void loadpushConfig(){
        DialogFragmentHelper.showLoadingDialog(getSupportFragmentManager(),"");
        loadPushConfigCommand = ProtoRequestHelper.task(ProtoHttpFactory.getPushConfig()).setTag(this.hashCode()).request(this);
    }
    private void setPushConfig(){
        setPushConfigCommand = ProtoRequestHelper.task(ProtoHttpFactory.setPushConfig(getDataByCheckStatus(cb_live),getDataByCheckStatus(cb_follow),getDataByCheckStatus(cb_call_live))).setTag(this.hashCode()).request(this);
    }


    @Override
    public void onRequestSuccess(String command, String data) {
        if (command.equals(loadPushConfigCommand)){
            DialogFragmentHelper.closeLoadingDialog(getSupportFragmentManager());
            Result<PushConfigModel> result = JSON.parseObject(data,new TypeReference<Result<PushConfigModel>>(){}.getType());
            if (result.result==CodeSuccess){
                pushConfigModel = result.data;
                refreshView();
            }
        }else if(command.equals(setPushConfigCommand)){
            ResultNoData result = JSON.parseObject(data,new TypeReference<Result>(){}.getType());
            if (result.result==CodeSuccess){
                pushConfigModel.setPushFollowLive(getDataByCheckStatus(cb_live));
                pushConfigModel.setPushNewFollower(getDataByCheckStatus(cb_follow));
                pushConfigModel.setPushCallLive(getDataByCheckStatus(cb_call_live));
                refreshView();
            }else{
                refreshView();
            }
        }

    }

    @Override
    public void onRequestError(String command, int code, String message) {
        if(command.equals(setPushConfigCommand)){
            refreshView();
        }

    }

    private int getDataByCheckStatus(CheckBox aSwitch){
        return aSwitch.isChecked()?1:0;
    }


    private void refreshView(){
        cb_live.setChecked(pushConfigModel.isLiveOpen());
        cb_follow.setChecked(pushConfigModel.isFollowOpen());
        cb_call_live.setChecked(pushConfigModel.isPushCallLiveOpen());
    }

    private void refreshFamilyMute(String familyId){
        Team team = TeamDataCache.getInstance().getTeamById(familyId);
        if (team != null){
            cb_family_message.setChecked(!team.mute());
        }


    }

    private void initFamilyMute(){
        if (FamilyAccountManager.getInstance().isHasFamilyUserInfo()) {
            final FamilyUserInfo.FamilyUserBean familyUserBean = FamilyAccountManager.getInstance().getFamilyUserInfo().getFamilyUser();
            Team team = TeamDataCache.getInstance().getTeamById(familyUserBean.getTid());
            if (team != null){
                $(R.id.rl_family_message).setVisibility(View.VISIBLE);
                cb_family_message.setChecked(!team.mute());
                cb_family_message.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        setFamilyMute(familyUserBean.getTid());
                    }
                });
            }
        }
    }

    private void setFamilyMute(final String familyId){

        Team team = TeamDataCache.getInstance().getTeamById(familyId);
        if (team == null)return ;
        NIMClient.getService(TeamService.class).muteTeam(familyId,!team.mute()).setCallback(new RequestCallback<Void>() {
            @Override
            public void onSuccess(Void param) {
                refreshFamilyMute(familyId);
            }
            @Override
            public void onFailed(int code) {
                refreshFamilyMute(familyId);
            }
            @Override
            public void onException(Throwable exception) {
                refreshFamilyMute(familyId);
            }
        });
    }
}
